package com.travel.server.manager.impl.command.article;

import com.travel.common.CommonConstant;
import com.travel.server.manager.dto.request.article.UpdateArticleRequest;
import com.travel.server.manager.dto.response.EmptyResponse;
import com.travel.server.manager.impl.command.AbstractCommand;
import com.travel.server.manager.impl.dal.ArticleDal;
import com.travel.server.manager.model.ArticleInfo;
import com.travel.server.manager.model.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @Author: LiZhenhui
 * @Date: 2018/12/20 16:10
 * @Desc:
 */
@Component
public class UpdateArticleCommand extends AbstractCommand<UpdateArticleRequest, EmptyResponse> {

    @Autowired
    private ArticleDal articleDal;

    @Override
    protected void checkInput(UpdateArticleRequest request) {
    }

    @Override
    public EmptyResponse handle(UpdateArticleRequest request) {
        Date now = new Date();
        UserInfo userInfo = getUserInfo();

        ArticleInfo articleInfo;
        if(request.getId() == null){
            articleInfo = new ArticleInfo();
            articleInfo.setCreateBy(userInfo.getName());
            articleInfo.setCreateTime(now);
            articleInfo.setState(CommonConstant.YES);
        }else {
            articleInfo = articleDal.selectById(request.getId());
        }
        articleInfo.setArticleTitle(request.getTitle());
        articleInfo.setClassId(request.getClassId());
        articleInfo.setContent(request.getContent());
        articleInfo.setIsPublish(request.getIsPublish());
        articleInfo.setUpdateBy(userInfo.getName());
        articleInfo.setUpdateTime(now);
        articleDal.insertOrUpdate(articleInfo);
        return new EmptyResponse();
    }
}
